/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.gui;

import java.util.Iterator;

import javax.swing.tree.DefaultMutableTreeNode;

import alloy.api.LiftedUnsatCore;
import alloy.ast.ASTDepthFirstReturnVisitor;
import alloy.ast.Formula;
import alloy.ast.FormulaSeq;
import alloy.ast.Formulas;
import alloy.ast.Leaf;
import alloy.ast.Location;
import alloy.ast.Node;

class VerySimpleSwingASTVisitor extends ASTDepthFirstReturnVisitor {
    
    private LiftedUnsatCore _core;

    public VerySimpleSwingASTVisitor(LiftedUnsatCore core_) {
	_core = core_;
    }

    static DefaultMutableTreeNode buildSwingAST(LiftedUnsatCore core_, Formula formula_) {
	VerySimpleSwingASTVisitor visitor = new VerySimpleSwingASTVisitor(core_);
	
	return (DefaultMutableTreeNode)formula_.applyReturnVisitor(visitor);
    }  // buildSwingAST()

    /**
     * Construct a Swing representation of the AST rooted at the given {@link Node}.
     *
     * @param node_ AST formula node
     * @return root of a newly allocated Swing representation of the AST.
     */
    public Object visit(Node node_) {
	//
	// allocate the Swing node which will be the root of the Swing representation
	// of the AST subtree rooted at node_.  as the user object of the Swing node,
	// we set a NodeInfo object indicating which AST node this Swing node represents.
	// this is used in highlighting the appropriate source text when the user
	// clicks on a Swing AST node.
	//
	DefaultMutableTreeNode swingNode = new DefaultMutableTreeNode
					       (new NodeInfo(node_.fullStringShortNames(), node_));

	//
	// recursively compute Swing representations of child nodes,
	// and add them as children of swingNode
	//
	if (!(node_ instanceof Leaf)) {
	    for (Iterator i = node_.getChildren(); i.hasNext(); ) {
		Node curChild = (Node)i.next();
		DefaultMutableTreeNode childSwingNode =
		    (DefaultMutableTreeNode)curChild.applyReturnVisitor(this);
		//addChildNode (swingNode, childSwingNode);
		swingNode.add (childSwingNode);
	    }
	}

	if (_core.isInCore(node_)) {
	    ((NodeInfo)swingNode.getUserObject()).markFalse();
	}

	
	//
	// If node_ has an annotation, put that annotation into the
	// Swing representation of the node.  We create a new
	// DefaultMutableTreeNode to hold the annotation,
	// and the only child of that node is the Swing representation
	// of node_.
	//
	// Annotations generally describe the overall
	// structural role of the node, e.g. "the conjunction of all facts"
	// or "inlining of function X".  Annotations makes it easier for the
	// user to make sense of the various parts of the Swing representation
	// of the AST.
	//
	String annotation = node_.getAnnotation();
	//System.out.println("annote: " + annotation);
 	if (annotation != null && annotation.trim().length() > 0) {
	    NodeInfo nodeInfo = (NodeInfo)swingNode.getUserObject();
	    nodeInfo.setHasAnnotation(true);

	    //if (oneValue != null) annotation = oneValue + " " + annotation;
	    NodeInfo ni = new NodeInfo("NOTE: " + annotation, node_);
	    if (_core.isInCore(node_)) {
		ni.markFalse();
	    }
	    ni.setIsAnnotation(true);
	    ni.setUsedVarIds (nodeInfo.getUsedVarIds());
	    ni.setValueTreeComputed (nodeInfo.valueTreeComputed());
	    //if (oneValue != null && oneValue.equals("F"))
		// mark failed constraints to be painted in red
		//ni.markFalse();
	    DefaultMutableTreeNode annoteNode = new DefaultMutableTreeNode(ni);
	    annoteNode.add(swingNode);
	    
	    swingNode = annoteNode;
	}
	
	return swingNode;
    }

    /** for FormulaSeq nodes, just view the contained formulas */
    public Object visit(final FormulaSeq formulaSeq_) {
	//Dbg.chk(!_visited.contains(formulaSeq_));
	//_visited.add(formulaSeq_);
	Formulas formulas = formulaSeq_.getFormulas();
	// annotate with annotation of formulaSeq_
	formulas.annotate(formulaSeq_.getAnnotation());
        // take location from formula sequence if no location
        // for formulas
        if ((formulaSeq_.getLocation() != Location.UNKNOWN) &&
            (formulas.getLocation() == Location.UNKNOWN))
            formulas.setLocation(formulaSeq_.getLocation());
	return formulas.applyReturnVisitor(this);
    }
    
}  // class SwingASTVisitor
